package com.neu.yth.nepBackend.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.neu.yth.nepBackend.entity.AqiFeedback;
import com.neu.yth.nepBackend.entity.Statistics;
import com.neu.yth.nepBackend.enums.ResponseEnum;
import com.neu.yth.nepBackend.statistics.mapper.StatisticsMapper;
import com.neu.yth.nepBackend.statistics.service.IStatisticsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neu.yth.nepBackend.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.Calendar;
import java.util.List;

/**
 * <p>
 * 空气质量监测数据统计表 服务实现类
 * </p>
 *
 * @author YTH
 * @since 2024-05-31
 */
@Service
public class StatisticsServiceImpl extends ServiceImpl<StatisticsMapper, Statistics> implements IStatisticsService {

    @Autowired
    StatisticsMapper statisticsMapper;

    @Autowired
    WebClient webClient;

    /**
     * 根据id查询
     *
     * @param statisticsId
     * @return
     */
    @Override
    public Result selectById(Integer statisticsId) {
        if (statisticsId == null){
            return Result.fail(500, "id为空", null);
        }
        Statistics statistics = this.getById(statisticsId);
        if (statistics == null){
            return Result.fail(ResponseEnum.SELECT_FAILED);
        }
        return Result.success(statistics);

    }

    /**
     * 查询所有
     *
     * @param gmId
     * @return
     */
    @Override
    public Result selectAll(Integer gmId) {
        if (gmId == null){
            return Result.fail(500, "gmId为空", null);
        }
        LambdaQueryWrapper<Statistics> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Statistics::getGmId, gmId);
        List<Statistics> statisticsList = this.list(wrapper);
        if (statisticsList == null || statisticsList.size() == 0){
            return Result.fail(ResponseEnum.RESULT_IS_EMPTY);
        }
        return Result.success(statisticsList);
    }

    /**
     * 提交
     *
     * @param statistics
     * @return
     */
    @Override
    @Transactional
    public Result submit(Statistics statistics,String token) {
        statistics.setId(null);
        Calendar calendar = Calendar.getInstance();
        statistics.setConfirmDate(calendar.getTime());
        boolean result = statisticsMapper.insert(statistics) > 0;
        if (result){
            AqiFeedback aqiFeedback = new AqiFeedback();
            aqiFeedback.setAfId(statistics.getAfId());
            aqiFeedback.setState(2);
            Result res = webClient.put()
                    .uri("/aqiFeedback/update")
                    .header("Authorization",token)
                    .bodyValue(aqiFeedback)
                    .retrieve()
                    .bodyToMono(Result.class)
                    .block();
//            System.out.println(res);
            if (res.getCode() == 200){
                return Result.success(statistics);
            }else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        return Result.fail(ResponseEnum.INSERT_FAILED);
    }

    /**
     * 更新
     *
     * @param statistics
     * @return
     */
    @Override
    public Result updateStatisticsById(Statistics statistics) {
        Calendar calendar = Calendar.getInstance();
        statistics.setConfirmDate(calendar.getTime());
        boolean result = this.updateById(statistics);
        if (result){
            return Result.success(statistics);
        }
        return Result.fail(ResponseEnum.UPDATE_FAILED);
    }

    /**
     * 根据afId查询
     *
     * @param afId
     * @return
     */
    @Override
    public Result selectByAfId(Integer afId) {
        if (afId == null){
            return Result.fail(500, "afId为空", null);
        }
        LambdaQueryWrapper<Statistics> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Statistics::getAfId, afId);
        Statistics statistic = statisticsMapper.selectOne(wrapper);
        if (statistic == null){
            return Result.fail(ResponseEnum.RESULT_IS_EMPTY);
        }
        return Result.success(statistic);
    }


}
